/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.support.scripting;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

/**
 * This class implements a compiled script running the main method of a
 * Cafesterol-compiled source.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
final class OCamlCompiledScript extends CompiledScript {

    /** Script engine that created this instance. */
    private final ScriptEngine engine;

    /** Main script method. */
    private final Method main;

    /** Script context. */
    private final ScriptContext context;

    /**
     * Constructs a new compiled script.
     * @param engine parent script engine - should not be <tt>null</tt>
     * @param main main script method - should not be <tt>null</tt>
     * @param context script context - should not be <tt>null</tt>
     */
    OCamlCompiledScript(final ScriptEngine engine,
                        final Method main,
                        final ScriptContext context) {
        assert engine != null : "null engine";
        assert main != null : "null main";
        assert context != null : "null context";
        this.engine = engine;
        this.main = main;
        this.context = context;
    } // end constructor(ScriptEngine, Method, ScriptContext)

    /**
     * {@inheritDoc}
     */
    @Override
    public Object eval(final ScriptContext context) throws ScriptException {
        return evaluate(OCamlContext.instance(context));
    } // end method 'eval(ScriptContext)'

    /**
     * {@inheritDoc}
     */
    @Override
    public Object eval(final Bindings bindings) throws ScriptException {
        return evaluate(new OCamlContext(OCamlContext.instance(this.context),
                                         OCamlBindings.instance(bindings)));
    } // end method 'eval(Bindings)'

    /**
     * {@inheritDoc}
     */
    @Override
    public Object eval() throws ScriptException {
        return evaluate(OCamlContext.instance(this.context));
    } // end method 'eval()'

    /**
     * {@inheritDoc}
     */
    @Override
    public ScriptEngine getEngine() {
        return this.engine;
    } // end method 'getEngine()'

    /**
     * Actually evaluates the underlying script, using the passed context.
     * @param context script context - should not be <tt>null</tt>
     * @return result of evaluation
     * @throws ScriptException if an error occurs during compilation or
     *                         execution
     */
    private Object evaluate(final OCamlContext context)
        throws ScriptException {
        assert context != null : "null context";
        try {
            return this.main.invoke(null,
                                    new String[0],
                                    context.getBindings(),
                                    context.getStdIn(),
                                    context.getStdOut(),
                                    context.getStdErr());
        } catch (final IllegalAccessException iae) {
            throw new ScriptException(iae);
        } catch (final InvocationTargetException ite) {
            throw new ScriptException(ite);
        } // end try/catch
    } // end method 'evaluate(OCamlContext)'

} // end class 'OCamlCompiledScript'
